<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/config.jsp" %>
<html>
<head>
    <title>查看详情</title>
</head>
<body>
<%
    String action = request.getParameter("action");
    String id = request.getParameter("id");
    String name = "";
    String password = "";
    String phone = "";
    String clazz = "";
    String homePath = "";
    String actionCn = "";
    switch (action) {
        case "edit": {
            actionCn = "编辑";
        }
        case "view": {
            if ("".equals(actionCn)) {
                actionCn = "查看";
            }
            if (id == "" || "".equals(id)) {
                out.print("不是正确的学生学号,页面将在5秒内跳转到学生列表页面。");
                out.print("<a href=\"query.jsp\">立即跳转</a>");
                response.setHeader("refresh", "5;URL=query.jsp");
            } else {
                String sql = "select * from t_user where id=?";
                PreparedStatement pstmt=conn.prepareStatement(sql);
                pstmt.setString(1,id);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    name = rs.getString("NAME");
                    password = rs.getString("PASSWORD");
                    phone = rs.getString("PHONE");
                    clazz = rs.getString("CLAZZ");
                    homePath = rs.getString("HOME_PATH");
                } else {
                    out.print("学生信息不存在");
                    out.print("<a href=\"query.jsp\">立即跳转</a>");
                    response.setHeader("refresh", "5;URL=query.jsp");
                }
                conn.close();
            }
            break;
        }
        case "add": {
            actionCn = "添加";
        }
        default: {
            if ("".equals(actionCn)) {
                actionCn = "添加";
            }
            String sql = "SELECT MAX(id)+1 as sno from t_user";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                id = rs.getString("sno");
            }
            conn.close();
            break;
        }

    }

%>


<center>
    <h1><%=actionCn%>学生</h1>
    <h3><span id="error" style="color: red">${msg}</span></h3>
    <form action="saveorUpdate.jsp?action=<%=action%>" method="post" onsubmit="return check();">
        <table border="1px" width="400px">
            <tr>
                <td width="10px">学号:</td>
                <td>
                    <input type="text" name="id" style="width: 100%;" value="<%=id%>" readonly>
                </td>
            </tr>
            <tr>
                <td>姓名:</td>
                <td>
                    <input type="text" name="name" id="name" value="<%=name%>" style="width: 100%;height: 100%">
                </td>
            </tr>
            <tr>
                <td>电话:</td>
                <td>
                    <input type="text" name="phone" id="phone" value="<%=phone%>" style="width: 100%;">
                </td>
            </tr>
            <tr>
                <td>密码:</td>
                <td>
                    <input type="password" name="password" id="password" style="width: 100%;">
                </td>
            </tr>
            <tr>
                <td>确认密码:</td>
                <td>
                    <input type="password" name="password2" id="password2" style="width: 100%;">
                </td>
            </tr>
            <tr>
                <td>班级:</td>
                <td>
                    <select name="clazz" style="width: 100%">
                        <option value="18电子商务技术1班" <%=clazz.equals("18电子商务技术1班") ? "selected" : ""%>>18电子商务技术1班</option>
                        <option value="18电子商务技术2班" <%=clazz.equals("18电子商务技术2班") ? "selected" : ""%>>18电子商务技术2班</option>
                        <option value="18计算机应用技术(数据应用)" <%=clazz.equals("18计算机应用技术(数据应用)") ? "selected" : ""%>>
                            18计算机应用技术(数据应用)
                        </option>
                        <option value="其他" <%=clazz.equals("其他") ? "selected" : ""%>>其他</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>根路径:</td>
                <td>
                    <input type="text" name="homePath" id="homePath" value="<%=homePath%>" style="width: 100%">
                </td>
            </tr>

            <tr>
                <%
                    if ("view".equals(action)) {
                %>
                <td colspan="2" align="center">
                    <a href="query.jsp">返回列表</a>
                </td>
                <%
                } else {
                %>
                <td colspan="2" align="center">
                    <input type="submit" value="保存">
                </td>
                <%
                    }
                %>
            </tr>
        </table>
    </form>
</center>

<script type="application/javascript">
    function check() {
        var error = document.getElementById("error");
        error.innerText = "";
        var name = document.getElementById("name").value;
        var patternName = /^[\u4e00-\u9fa5]{2,4}$/;
        if (!patternName.test(name)) {
            error.innerText = "请输入正确的中文姓名,2-4位汉字";
            return false;
        }

        var phone = document.getElementById("phone").value;
        var patternPhone = /^(13[0-9]|14[5|7]|15[0-9]|18[0|1|2|3|5|6|7|8|9]|177|166)\d{8}$/;
        if (!patternPhone.test(phone)) {
            error.innerText = "请输入正确中国大陆手机号码";
            return false;
        }
        var password = document.getElementById("password").value;
        var patternPassword = /^[0-9A-Za-z]{6,12}$/;
        if (!patternPassword.test(password)) {
            error.innerText = "密码是由数字大写字母,小写字母组成,长度为6-12位";
            return false;
        }
        var password2 = document.getElementById("password2").value;
        if (password != password2) {
            error.innerText = "两次密码不一致";
            return false;
        }
        return true;
    }
</script>


</body>
</html>
